<?php
namespace Admin\Controller;
use Think\Controller;

class IndexController extends Controller {

    public function index(){  
        $themes = cookie('oa_themes');
        if(!$themes){
            $themes = 'metro';
            cookie('oa_themes',$themes,3600);
        }

        $lang = $_GET['l'];
        switch ($lang) {
            case 'zh-cn':
                $this->lang = 'zh_CN';
                break;
            case 'zh-tw':
                $this->lang = 'zh_TW';
                break;            
            default:
                $this->lang = 'en';
                break;
        }
        $this->saveLang($lang);

        $this->themes = $themes;  
        $this->menu = L('TITLE_MENU');

        $this->close_current = L('CLOSE_CURRENT');
        $this->close_all = L('CLOSE_ALL');
        $this->close_other = L('CLOSE_OTHER');
        $this->close_left = L('CLOSE_LEFT');
        $this->close_right = L('CLOSE_RIGHT');

        $this->display();
    }

    public function getThemes()
    {
        $data = array(
                array(id => 'metro', text => 'metro'),
                array(id => 'gray', text => 'gray'),
                array(id => 'green', text => 'green'),
                array(id => 'red', text => 'red'),
                array(id => 'orange', text => 'orange'),
            );
        $this->ajaxReturn($data,'json');
    }

    public function saveThemes($themes='')
    {
        if($themes == ''){
            $themes = cookie('oa_themes');
            if(!$themes)
                $themes = 'metro';
        }
        cookie('oa_themes',$themes,3600);
    }

    public function getLanguage()
    {
        $lan = M('SysLanguage');
        $data = $lan->select();
        $this->ajaxReturn($data,'json');
    }

    public function saveLang($l)
    {
        cookie('oa_lang',$l,3600);
    }

    public function getLang()
    {
        //echo \Common\Common\WebAction::getLang();
        echo "string";
        echo L('QUERY_PARENTHESIS');
    }

    public function getModule()
    {
        $data = $this->getChilds(0);
        $this->ajaxReturn($data, 'json');
    }

    protected function getChilds($pid)
    {
        $data = array();
        $lang = \Common\Common\WebAction::getLangField();
        $sql = 'select `parentid`,`moduleid`,`moduletype`,`url`,`target`,`icon`,IFNULL(`' . $lang . '`,modulename) as modulename'
            . ' from sys_module where `parentid`=' . $pid . ' order by sort';
        $module = M();
        $childs = $module->query($sql);

        if(count($childs)>0){
            foreach ($childs as $child) {
                $data_child = array(
                    'id'        => $child[moduleid], 
                    'text'      => $child[modulename], 
                    'iconCls'   => $child[icon]
                );
                if($child[moduletype]==0){
                    $data_child[children] = $this->getChilds($child[moduleid]);
                }else{
                    $data_child[attributes] = array(
                            'url'       => $child[url], 
                            'target'    => $child[target], 
                            'params'    => $child[params], 
                            'icon'    => $child[icon]
                        );
                }
                array_push($data, $data_child);
            }
        }

        return $data;
    }
}